home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / x2ftp / msdos / source / swaga-c / copymove.swg / 0002_Copy File #2.pas < prev    next >
Pascal/Delphi Source File  |  1993-05-28  |  4KB  |  125 lines

  1. {I've been trying to figure out how to do a fairly fast copy
  2.  in pascal.  It doesn't have to be faster then Dos copy, but
  3.  I definatly DON'T want to shell out to Dos to do it!
  4.  I've got the following working... in the IDE of Turbo 6.0!
  5.  If I compile it, it wont work at all.  ALSO... If you COMP
  6.  the Files to check For errors, They are there.  (UGH!)
  7.  (ie, it isn't a perfect copy!)
  8.  The thing is I want to get as much as I can in each pass!
  9.  (But turbo has limits!)
  10.  Heres my code... Just rough, so no Real comments.
  11. }
  12.  
  13. Program Copy (InFile, OutFile);
  14.  
  15. Uses Dos;
  16.  
  17. Var
  18.    I, Count, BytesGot : Integer;
  19.    BP : Pointer;
  20.    InFile,OutFile:File;
  21.  
  22.    FI,FO : Word;
  23.  
  24.    Path,
  25.    FileName : String[80];
  26.  
  27.    DirInfo : SearchRec;
  28.    BaseRec, RecSize : longInt;
  29.  
  30. begin
  31.    FileName := ParamStr(1);             {Set the SOURCE as the first ParamSTR}
  32.    Path := ParamStr(2);                 {Set the Dest.  as the 2nd paramSTR}
  33.  
  34.    If paramCount = 0 Then
  35.       begin
  36.            Writeln('FastCopy (C) 1993 - Steven Shimatzki');
  37.            Writeln('Version : 3.0   Usage: FastCopy <Source> <Destination>');
  38.            Halt(1);
  39.       end;
  40.  
  41.    FindFirst(FileName,Archive,DirInfo);
  42.  
  43.    If DirInfo.Name <> '' Then
  44.    begin
  45.  
  46.        RecSize := MaxAvail - 1024;  {Get the most memory but leave some}
  47.        BaseRec := RecSize;
  48.  
  49.        If RecSize > DirInfo.Size Then      {If a "SMALL" File, gobble it up}
  50.            RecSize := DirInfo.Size;        {In one pass!  Size = Recordsize}
  51.  
  52.        Count := DirInfo.Size Div RecSize;  {Find out how many Passes!}
  53.  
  54.        GetMem (Bp, RecSize);   {Allocate memory to the dynamic Variable}
  55.  
  56.        Assign (InFile,FileName);       {Assign the File}
  57.        Assign (OutFile,Path);          {Assign the File}
  58.  
  59.        Filemode := 0;     {Open the INFile as READONLY}
  60.  
  61.        Reset(InFile,RecSize);      {open the input}
  62.        ReWrite(OutFile,RecSize);   {make the output}
  63.  
  64.  
  65.        For I := 1 to Count do    {Do it For COUNT passes!}
  66.        begin
  67.  
  68.             {$I-}
  69.             Blockread(InFile,BP^,1,BytesGot);   {Read 1 BLOCK}
  70.             {$I+}
  71.  
  72.             BlockWrite(outFile,BP^,1,BytesGot);   {Write 1 BLOCK}
  73.  
  74.             If BytesGot <> 1 Then
  75.                Writeln('Error!  Disk Full!');
  76.  
  77.        end;
  78.  
  79. {If not all read in, then I have to get the rest seperatly!  partial Record!}
  80.  
  81.        If Not ((Count * RecSize) = DirInfo.Size) Then
  82.        begin
  83.             RecSize := (DirInfo.Size - (Count * RecSize)) ;
  84.                        {^^^ How much is left to read? get it in one pass!}
  85.  
  86.  
  87.             FreeMem(Bp, BaseRec);      {Dump the mem back}
  88.             GetMem(Bp, RecSize);       {Get the new memory}
  89.  
  90.             FileMode := 0;         {Set input For readonly}
  91.  
  92.             Reset (InFile,1);
  93.  
  94.             Filemode := 2;         {Set output For Read/Write}
  95.  
  96.             Reset (OutFile,1);
  97.  
  98.             Seek(InFile, (Count * BaseRec));   {Move to old location}
  99.             Seek(OutFile, (Count * BaseRec));{ same }
  100.  
  101.             FI := FilePos(InFile);    {Just used to see where I am in the File}
  102.             FO := FilePos(OutFile);   {Under the Watch Window... Remove later}
  103.  
  104.             {$I-}
  105.             BlockRead(InFile,Bp^,RecSize,BytesGot);    {REad the File}
  106.             {$I+}
  107.  
  108.             BlockWrite(OutFile,Bp^,RecSize,BytesGot);  {Write the File}
  109.  
  110.        end;
  111.  
  112.        Close(OutFile);
  113.        Close(InFile);
  114.  
  115.        FreeMem (Bp,RecSize);
  116.  
  117.    end;
  118.  
  119. end.
  120.  
  121. {
  122. You don't close the input- and output File when your finished With the
  123. first count passes. Maybe your last block will not be written to disk,
  124. when you reopen the outputFile For writing. I can't see another problem
  125. right now.